<template>
  <el-dialog
    v-model="visible"
    :close-on-click-modal="false"
    @close="handleClose"
    width="900px"
    class="unified-dialog restart-history-detail-dialog"
    append-to-body
  >
    <template #header>
      <DialogHeader
        icon="InfoFilled"
        title="重启历史详情"
        description="查看完整的重启记录信息"
        type="primary"
      />
    </template>
    
    <div class="dialog-content" v-if="historyData">
      <!-- 基础信息 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>基础信息</span>
        </div>
        
        <el-descriptions :column="2" border>
          <el-descriptions-item label="记录ID">{{ historyData.id }}</el-descriptions-item>
          <el-descriptions-item label="重启类型">
            <el-tag size="small" :type="historyData.restart_type === 'JOB' ? 'success' : 'primary'">
              {{ historyData.restart_type === 'JOB' ? '作业' : '服务' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="操作类型">
            <el-tag size="small" :type="getActionTagType(historyData.action)">
              {{ getActionLabel(historyData.action) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag size="small" :type="getStatusTagType(historyData.status)">
              {{ getStatusLabel(historyData.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="触发者">{{ historyData.triggered_by || '-' }}</el-descriptions-item>
          <el-descriptions-item label="开始时间">{{ historyData.created_time || '-' }}</el-descriptions-item>
          <el-descriptions-item label="完成时间">{{ historyData.completed_time || '-' }}</el-descriptions-item>
          <el-descriptions-item label="耗时(秒)">
            {{ historyData.duration_seconds !== null && historyData.duration_seconds !== undefined ? historyData.duration_seconds : '-' }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- JOB 类型专用信息 -->
      <div class="detail-section" v-if="historyData.restart_type === 'JOB'">
        <div class="section-title">
          <el-icon><Folder /></el-icon>
          <span>作业信息</span>
        </div>
        
        <el-descriptions :column="2" border>
          <el-descriptions-item label="作业名称" :span="2">{{ historyData.job_name || '-' }}</el-descriptions-item>
          <el-descriptions-item label="作业ID">{{ historyData.job_id || '-' }}</el-descriptions-item>
          <el-descriptions-item label="JAR名称">{{ historyData.jar_name || '-' }}</el-descriptions-item>
          <el-descriptions-item label="JAR路径" :span="2">{{ historyData.jar_path || '-' }}</el-descriptions-item>
          <el-descriptions-item label="入口类" :span="2">{{ historyData.entry_class || '-' }}</el-descriptions-item>
          <el-descriptions-item label="程序参数" :span="2">
            <el-input
              :value="historyData.program_args || '-'"
              type="textarea"
              :rows="3"
              readonly
            />
          </el-descriptions-item>
          <el-descriptions-item label="Savepoint路径" :span="2">{{ historyData.savepoint_path || '-' }}</el-descriptions-item>
          <el-descriptions-item label="使用Savepoint">
            <el-tag size="small" :type="historyData.use_savepoint ? 'success' : 'info'">
              {{ historyData.use_savepoint ? '是' : '否' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="恢复原因" :span="2">{{ historyData.recovery_reason || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- SERVICE 类型专用信息 -->
      <div class="detail-section" v-if="historyData.restart_type === 'SERVICE'">
        <div class="section-title">
          <el-icon><Monitor /></el-icon>
          <span>服务信息</span>
        </div>
        
        <el-descriptions :column="2" border>
          <el-descriptions-item label="主机名">{{ historyData.host || '-' }}</el-descriptions-item>
          <el-descriptions-item label="服务类型">
            <el-tag size="small" type="info">{{ historyData.service_type || '-' }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 错误信息 -->
      <div class="detail-section" v-if="historyData.error_message">
        <div class="section-title">
          <el-icon><Warning /></el-icon>
          <span>错误信息</span>
        </div>
        
        <el-descriptions :column="1" border>
          <el-descriptions-item label="错误消息" :span="1">
            <div class="error-message-content">
              {{ historyData.error_message }}
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 原始失败原因 -->
      <div class="detail-section" v-if="historyData.original_failure_reason">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>原始失败原因</span>
        </div>
        
        <el-descriptions :column="1" border>
          <el-descriptions-item label="失败原因">{{ historyData.original_failure_reason }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import {
  InfoFilled, Document, Folder, Monitor, Warning
} from '@element-plus/icons-vue'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  historyData: {
    type: Object,
    default: null
  }
})

const emit = defineEmits(['update:modelValue'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const handleClose = () => {
  visible.value = false
}

// 获取操作类型标签样式
const getActionTagType = (action) => {
  const typeMap = {
    'RESTART': 'warning',
    'START': 'success',
    'STOP': 'danger',
    'RECOVER': 'primary'
  }
  return typeMap[action] || ''
}

// 获取操作类型标签文本
const getActionLabel = (action) => {
  const labelMap = {
    'RESTART': '重启',
    'START': '启动',
    'STOP': '停止',
    'RECOVER': '恢复'
  }
  return labelMap[action] || action
}

// 获取状态标签样式
const getStatusTagType = (status) => {
  const typeMap = {
    'SUCCESS': 'success',
    'FAILED': 'danger',
    'ATTEMPTING': 'warning'
  }
  return typeMap[status] || ''
}

// 获取状态标签文本
const getStatusLabel = (status) => {
  const labelMap = {
    'SUCCESS': '成功',
    'FAILED': '失败',
    'ATTEMPTING': '进行中'
  }
  return labelMap[status] || status
}
</script>

<style scoped>
.restart-history-detail-dialog {
  border-radius: 12px;
}

.detail-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.detail-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.detail-section:last-child {
  margin-bottom: 0;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.section-title .el-icon {
  margin-right: 8px;
  font-size: 18px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-item.full-width {
  grid-column: 1 / -1;
}

.detail-label {
  font-size: 13px;
  font-weight: 600;
  color: #909399;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.detail-value {
  font-size: 14px;
  color: #303133;
  word-break: break-word;
}

.error-message-content {
  background: #fff5f5;
  border: 1px solid #f56c6c;
  border-radius: 4px;
  padding: 12px;
  color: #f56c6c;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid #e4e7ed;
}

</style>

